package com.beyondprototype.batch.config;

import java.util.List;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.json.JacksonJsonObjectReader;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.JsonObjectReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.mongodb.core.MongoTemplate;

import com.beyondprototype.batch.model.PremierCustomer;
import com.beyondprototype.batch.service.PremierMarkerService;
import com.fasterxml.jackson.databind.DeserializationFeature;
//import com.beyondprototype.batch.service.PremierMarkerService;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class GcduJasonDataBatchConfiguration {
    
    private String gcduDataFile = "/Users/xwp/Desktop/cngit/springbatch/usecase/gcdu-premier-marker-data.json";
    
    
    @Autowired
    private PremierMarkerService premierMarkerService;
   
    
    @Bean
    public JsonObjectReader<PremierCustomer> objectReader() {
    	
    	ObjectMapper objectMapper = new ObjectMapper();
    	
    	objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    	
    	JacksonJsonObjectReader<PremierCustomer> reader = new JacksonJsonObjectReader<>(PremierCustomer.class);
    	
    	reader.setMapper(objectMapper);
    	
    	return reader;
    	
    }
    
    @Bean
    @Qualifier("gcduJsonItemReader")
    public ItemReader<PremierCustomer> gcduItemReader(JsonObjectReader<PremierCustomer> objectReader) {
    	
        JsonItemReader<PremierCustomer> reader = new JsonItemReader<PremierCustomer>();
        reader.setResource(new FileSystemResource(gcduDataFile));
        reader.setName("PremierMarker");
        reader.setJsonObjectReader(objectReader);
        
        return reader;
    }
    
    //insert customer linkage records into mongodb
    @Bean 
    @Qualifier("gcduJsonItemWriter")
    public ItemWriter<PremierCustomer> gcduItemWriter() {
        return new ItemWriter<PremierCustomer>() {
            @Override
            public void write(List<? extends PremierCustomer> items) throws Exception {
                for (PremierCustomer item : items) {
                	log.info("Writing "+ item.toString());
                }
                
                premierMarkerService.create(items);
            }
        };
    }

    @Bean
    @Qualifier("gcduJsonItemProcessor")
    public ItemProcessor<PremierCustomer, PremierCustomer> gcduItemProcessor() {
        return new ItemProcessor<PremierCustomer, PremierCustomer>() {
            @Override
            public PremierCustomer process(PremierCustomer item) throws Exception {
            	
            	log.info("Processing "+item.toString());
            	
            	item.setStatus("Processing");
            	
                return item;
            }
        };
    }
}
